package com.inspinia.base.common.model;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

/**
 * 用户登录事件管理的类
 * Created by veblen on 2017/2/24.
 */
public class UserLoginEventManager {
    private static Collection listeners;

    /**
     * 添加事件
     *
     * @param listener LoginListener
     */
    public static void addLoginListener(LoginListener listener) {
        if (listeners == null) {
            listeners = new HashSet();
        }
        listeners.add(listener);
    }

    /**
     * 移除事件
     *
     * @param listener LoginListener
     */
    public static void removeLoginListener(LoginListener listener) {
        if (listeners == null) {
            return;
        }
        listeners.remove(listener);
    }


    /**
     * 通知所有的LoginListener
     */
    public static void notifyBeforeLogin(UserLoginEvent event) {
        Iterator iter = listeners.iterator();
        while (iter.hasNext()) {
            LoginListener listener = (LoginListener) iter.next();
            listener.beforeLogin(event);
        }
    }

    /**
     * 通知所有的LoginListener
     */
    public static void notifyAfterLogin(UserLoginEvent event) {
        if (null != listeners) {
            Iterator iter = listeners.iterator();
            while (iter.hasNext()) {
                LoginListener listener = (LoginListener) iter.next();
                listener.afterLogin(event);
            }
        }
    }
}